package com.gc.web.controller.business;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gc.business.domain.IntegralConfig;
import com.gc.business.service.IIntegralConfigService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import com.gc.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 积分配置 控制层
 * 
 * @author liujinteng
 * @date 2021-01-04
 */
@Slf4j
@Controller
@RequestMapping("/business/integralConfig")
public class IntegralConfigController extends BaseController {
    private String prefix = "business/integralConfig";
	
	private final IIntegralConfigService integralConfigService;

	@Autowired
	public IntegralConfigController(IIntegralConfigService integralConfigService) {
		this.integralConfigService = integralConfigService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:integralConfig:view")
	@GetMapping
	public String integralConfig(){
	    return prefix + "/integralConfig";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{configId}")
	public String edit(@PathVariable("configId") Long configId, ModelMap mmap){
		IntegralConfig integralConfig = integralConfigService.getById(configId);
		mmap.put("integralConfig", integralConfig);
		return prefix + "/edit";
	}
	
	/**
	 * 查询积分配置列表
	 */
	@RequiresPermissions("business:integralConfig:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(IntegralConfig integralConfig){
		startPage();
        List<IntegralConfig> list = integralConfigService.list(integralConfig);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存积分配置
	 */
	@RequiresPermissions("business:integralConfig:add")
	@Log(title = "积分配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(IntegralConfig integralConfig){
		SysUser sysUser = ShiroUtils.getSysUser();
		integralConfig.setCompanyId(sysUser.getAttrId());
		integralConfig.setCreateBy(sysUser.getUserName());
		integralConfig.setCreateTime(DateUtil.date());
		integralConfigService.save(integralConfig);
		return Result.success();
	}

	/**
	 * 修改保存积分配置
	 */
	@RequiresPermissions("business:integralConfig:edit")
	@Log(title = "积分配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(IntegralConfig integralConfig){
		SysUser sysUser = ShiroUtils.getSysUser();
		UpdateWrapper<IntegralConfig> wrapper = new UpdateWrapper<>();
		wrapper.eq("config_id",integralConfig.getConfigId());
        if(StringUtils.isNotEmpty(integralConfig.getReportFault())){
            wrapper.set("report_fault", integralConfig.getReportFault());
        }
        if(StringUtils.isNotEmpty(integralConfig.getReportTiring())){
            wrapper.set("report_tiring", integralConfig.getReportTiring());
        }
        if(StringUtils.isNotEmpty(integralConfig.getFaultRepair())){
            wrapper.set("fault_repair", integralConfig.getFaultRepair());
        }
        if(StringUtils.isNotEmpty(integralConfig.getElevatorUpkeep())){
            wrapper.set("elevator_upkeep", integralConfig.getElevatorUpkeep());
        }
        if(StringUtils.isNotEmpty(integralConfig.getIntegralRules())){
            wrapper.set("integral_rules", integralConfig.getIntegralRules());
        }
        wrapper.set("update_by", sysUser.getUserName());
        wrapper.set("update_time", DateUtil.date());
		integralConfigService.update(wrapper);
		return Result.success();
	}
	
	/**
	 * 删除积分配置
	 */
	@RequiresPermissions("business:integralConfig:remove")
	@Log(title = "积分配置", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		integralConfigService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出积分配置列表
	 */
	@RequiresPermissions("business:integralConfig:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(IntegralConfig integralConfig){
		List<IntegralConfig> list = integralConfigService.list(integralConfig);
		ExcelUtil<IntegralConfig> util = new ExcelUtil<>(IntegralConfig.class);
		util.exportExcel(list, "integralConfig");
		return Result.success();
	}

	/**
	 * 跳转页
	 */
	@RequiresPermissions("business:integralConfig:skip")
	@GetMapping("/skip")
	public String integralConfigSkip(ModelMap mmap) {
		LambdaQueryWrapper<IntegralConfig> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(IntegralConfig::getCompanyId, ShiroUtils.getSysUser().getAttrId());
		IntegralConfig integralConfig = integralConfigService.getOne(wrapper);
		if (integralConfig == null) {
			mmap.put("configId", IdUtils.getPrimaryKey());
			return prefix + "/add";
		} else {
			mmap.put("integralConfig", integralConfig);
			return prefix + "/edit";
		}
	}
}
